<?php

namespace App\Http\Controllers\Admin;

use App\Models\Banner;
use App\Models\Problems;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BannerController extends Controller
{


    /**
     * 后台用户
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {


        return view('admin/banner/index');

    }

    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $params = $request->only(['id']);
        $validator = \Validator::make($params, [
            'id' => 'required|exists:banner,id',

        ], [
            'id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = Banner::find($request->id);

        return view('admin/banner/edit', ['data' => $data]);
    }

    /**
     * 添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {


        return view('admin/banner/add');
    }


    /**
     * 帮助中心
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
//        $searchParams = json_decode($request->searchParams, true);
        $data = Banner::paginate($request->limit)->toArray();

        return $this->success(['data' => $data['data'], 'count' => $data['total'], 'code' => 0]);
    }

    /**
     * 帮助详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $params = $request->only(['id']);
        $validator = \Validator::make($params, [
            'id' => 'required|exists:problems,id',
        ], [
            'problems_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $problem = Problems::find($request->problems_id);

        return $this->success($problem);
    }


    /**
     * 新增常见问题
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $params = $request->only(['img_url', 'path']);
        $validator = \Validator::make($params, [
            'path' => 'required|string|max:100',
            'img_url' => 'required',
        ], [
            'path.*' => '标题参数错误',
            'img_url.*' => '内容参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = [
            'path' => $request->path,
            'img_url' => $request->img_url,
        ];
        $re = Banner::create($data);
        if ($re) {
            return $this->success(['msg' => '添加成功']);
        }
        return $this->error('创建失败');
    }


    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(Request $request)
    {
        $params = $request->only(['path', 'img_url', 'id']);
        $validator = \Validator::make($params, [
            'id' => 'required|exists:problems,id',
            'path' => 'required|string|max:100',
            'img_url' => 'required',
        ], [
            'id.*' => '参数错误',
            'path.*' => '参数错误',
            'img_url.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $re = Banner::find($request->id);
        $re->path = $request->path;
        $re->img_url = $request->img_url;

        if ($re->save()) {
            return $this->success(['msg' => '成功']);
        }
        return $this->error('修改失败');

    }

    /**
     * 删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        $params = $request->only(['id']);
        $validator = \Validator::make($params, [
            'id' => 'required|exists:banner,id',
        ], [
            'id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $problems = Banner::find($request->id);

        if($problems->delete())
        {
            return $this->success(['msg' => '删除成功']);
        }
        return $this->error('删除失败');


    }
}
